package com.example.studentregistration.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.studentregistration.mapper.ReportingMapper;
import com.example.studentregistration.pojo.Reporting;
import com.example.studentregistration.service.ReportingService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Service
public class ReportingServiceImpl implements ReportingService {

    @Autowired
    private ReportingMapper reportingMapper;

    @Override
    public Result<?> insertReporting(Reporting reporting) {
        int r = reportingMapper.insertReporting(reporting);
        return r > 0 ? Result.success("新增成功") : Result.fail("新增失败");
    }

    @Override
    public Result<?> updateReporting(Reporting reporting) {
        reporting.setUpdateTime(new Date());
        int r = reportingMapper.updateReporting(reporting);
        return r > 0 ? Result.success("修改成功") : Result.fail("修改失败");
    }

    @Override
    public Result<?> deleteReporting(Long id) {
        int r = reportingMapper.deleteReporting(id);
        return r > 0 ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<Reporting> queryReportingPage(Integer currentPage, Integer pageSize,
                                              String status, String name,
                                              String studentNo, String className,
                                              String buildingName, Integer isPaid) {
        PageHelper.startPage(currentPage, pageSize);
        return (Page<Reporting>) reportingMapper.queryReportingPage(status, name, studentNo, className, buildingName, isPaid);
    }

    @Override
    public Result<Reporting> queryReportingById(Long id) {
        Reporting r = reportingMapper.queryReportingById(id);
        return r != null ? Result.success(r) : Result.fail("记录不存在");
    }

    @Override
    public List<Reporting> queryAllReporting() {
        return reportingMapper.queryAllReporting();
    }

    @Override
    public void exportReportingExcel(HttpServletResponse response) throws IOException {
        // ① 查数据
        List<Reporting> list = reportingMapper.queryAllReporting();
        System.out.println(">>> 裸写条数：" + list.size());

        // ② 创建 writer
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.write(list, true);   // ③ 直接写，不设别名

        // ④ 输出到浏览器
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("报到信息_裸写", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);   // true=关闭流
        writer.close();
    }
}
